<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\Area;
use think\db\Query;
use think\Request;

class AreaController extends BaseController
{
	
	function getHotCity()
	{
		$arr = [['id'=>2,'name'=>'北京'],['id'=>802,'name'=>'上海'],['id'=>1965,'name'=>'广州'],['id'=>1988,'name'=>'深圳']];
		$this->writeJson($arr);
	}
	
	function getRegion()
	{
		$parent_id  = $this->request->get('parent_id/d',0);
		$keyword  = $this->request->get('keyword');
		
		$rows = Area::when(!empty($parent_id),function (Query $query)use($parent_id){
			$query->where(['pid'=>$parent_id]);
		},function (Query $query){
			$query->where(['level'=>2])->group('first')->order(['first'=>'asc']);
		})->when(!empty($keyword),function (Query $query)use($keyword){
			$query->whereLike('name','%'.$keyword.'%');
		})->select();
		$data = [];
		foreach ($rows as $row){
			$data[$row->first] = Area::where(['level'=>2,'first'=>$row->first])->select();
		}
		$this->writeJson($data);
	}
	
	function getProvince()
	{
		$rows = Area::where(['level'=>1])->select();
		$this->writeJson($rows);
	}
	
	function getCity()
	{
		$parent_id  = $this->request->get('parent_id/d');
		$rows = Area::where(['pid'=>$parent_id])->select();
		$this->writeJson($rows);
	}
	function getDistrict()
	{
		$parent_id  = $this->request->get('parent_id/d');
		$rows = Area::where(['pid'=>$parent_id])->select();
		$this->writeJson($rows);
	}
	
	

}
